package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.api.common.CommonProblemClassSelectDto;
import com.dog.basics.bean.model.common.SysCommonProblemClass;
import com.dog.basics.bean.vo.admin.sys.AdminCommonProblemClassPageListVo;
import com.dog.basics.dao.common.SysCommonProblemClassMapper;
import com.dog.basics.service.common.ISysCommonProblemClassService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 常见问题分类 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-10-11
 */
@Service
@AllArgsConstructor
public class SysCommonProblemClassServiceImpl extends ServiceImpl<SysCommonProblemClassMapper, SysCommonProblemClass>
        implements ISysCommonProblemClassService {


    /**
     * 常见问题列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<AdminCommonProblemClassPageListVo> getCommonProblemClassPageList(Page page, CommonProblemClassSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblemClass>query().lambda()
                .eq(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblemClass::getTitle,
                        dto.getTitle()));
        List<SysCommonProblemClass> records = page.getRecords();

        Validator.validateNotNull(records, "common.noData");

        page.setRecords(records.stream().map(problemClass -> BeanUtil.toBean(problemClass, AdminCommonProblemClassPageListVo.class)).collect(Collectors.toList()));

        return page;
    }


}
